﻿using LogisticsTrackingWebApi_V3.Models;
using LogisticsTrackingWebApi_V3.Services;

namespace LogisticsTrackingWebApi_V3.ServicesImpl.Decorators
{

    /// <summary>
    /// 日志装饰器 - 为物流查询添加日志功能
    /// </summary>
    public class LoggingDecorator : ILogisticsProvider
    {
        private readonly ILogisticsProvider _provider;
        private readonly ILogger<LoggingDecorator> _logger;

        public LoggingDecorator(ILogisticsProvider provider, ILogger<LoggingDecorator> logger)
        {
            _provider = provider;
            _logger = logger;
        }

        public string CompanyCode => _provider.CompanyCode;
        public string CompanyName => _provider.CompanyName;

        public async Task<LogisticsInfo> QueryTrackingAsync(string trackingNumber)
        {
            _logger.LogInformation("开始查询物流信息，公司：{CompanyName}，运单号：{TrackingNumber}",
                _provider.CompanyName, trackingNumber);

            try
            {
                var result = await _provider.QueryTrackingAsync(trackingNumber);
                _logger.LogInformation("物流信息查询成功，公司：{CompanyName}，运单号：{TrackingNumber}",
                    _provider.CompanyName, trackingNumber);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "物流信息查询失败，公司：{CompanyName}，运单号：{TrackingNumber}",
                    _provider.CompanyName, trackingNumber);
                throw;
            }
        }

        public bool MatchTrackingNumber(string trackingNumber)
        {
            return _provider.MatchTrackingNumber(trackingNumber);
        }
    }

}
